import java.util.Arrays;

/**
 * Created with Intellij IDEA.
 * Description
 * User: wen
 * Date:2022-02-22
 * TIME:0:33
 */
public class TestDemo {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        System.out.println(Arrays.binarySearch(arr, 4));
    }
    //=================================================================================//
    public static int binarySearch(int[] arr, int key){
        int left = 0;
        int right = arr.length - 1;
        while(left <= right){
            int mid = (left + right) / 2;
            if(arr[mid] > key){
                right = mid - 1;
            }else if(arr[mid] < key){
                left = mid + 1;
            }else{
                return mid;
            }
        }
        //数组中没有要找的元素，返回-1
        return -1;
    }
    public static void main16(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};//必须是有序数组
        System.out.println(binarySearch(arr, 4));
    }
    //=================================================================================//
    public static int findNum(int[] arr, int key){
        for (int i = 0; i < arr.length; i++) {
            if(key == arr[i]){
                return i;
            }
        }
        return -1;//为什么用-1，因为数组的下标，不可能存在负数。
    }
    public static void main15(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        System.out.println(findNum(arr, 4));
    }
    //=================================================================================//
    public static double ave(int[] arr){
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum = sum + arr[i];
        }
        return (double)sum/(double)arr.length;
    }
    public static void main14(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6};
        System.out.println(ave(arr));
    }
    //=================================================================================//
    public static int maxNum(int[] arr){
        //if(arr == null) return -1;
        //if(arr.length == 0) return -1;
        if((arr == null) || (arr.length == 0))
            return -1;
        //
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if(max < arr[i]){
                max = arr[i];
            }
        }
        return max;
    }
    public static void main13(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        int max = maxNum(arr);
        System.out.println(max);
    }
    //=================================================================================//
    public static String myTostring(int[] arr){
        if(arr == null){
            return null;
        }
        String str = "[";
        for (int i = 0; i < arr.length; i++) {
            // 借助 String += 进行拼接字符串
            str = str + arr[i];
            // 除了最后一个元素之外, 其他元素后面都要加上 ", "
            if(i != arr.length - 1){
                str = str + ",";
            }
        }
        str = str + "]";
        return str;
    }
    public static void main12(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        //int[] arr = null;
        System.out.println(myTostring(arr));
    }
    //---------------------------------------------------------------------//
    public static int[] transform(int[] arr){
        int[] ret1 = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ret1[i] = arr[i] * 2;
        }
        return ret1;
    }
    public static void main11(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        int[] ret2 = transform(arr);
        System.out.println(Arrays.toString(arr));
        System.out.println(Arrays.toString(ret2));
    }
    //---------------------------------------------------------------------//
    public static void transform1(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i] * 2;
        }
    }
    public static void main10(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        System.out.println(Arrays.toString(arr));
        transform(arr);
        System.out.println(Arrays.toString(arr));
    }
    //---------------------------------------------------------------------//
    public static void Swap(int[] arr){
        int tmp = arr[0];
        arr[0] = arr[1];
        arr[1] = tmp;
    }
    public static void main9(String[] args) {
        int[] arr = {10, 20};
        System.out.println("交换前" + arr[0] + " " + arr[1]);
        Swap(arr);
        System.out.println("交换后" + arr[0] + " " + arr[1]);
    }

    public static void main8(String[] args) {
        int[] array1 = {1, 2, 3, 4, 5, 6};
        int[] array2 = array1;
    }
    //---------------------------------------------------------------------//
    public static void func1(int[] array) {
        array = new int[]{11, 2, 13, 4, 15, 61};
    }
    public static void func2(int[] array) {
        array[0] = 889;
    }
    public static void main7(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6};
        System.out.println(Arrays.toString(array));
        func1(array);
        //func2(array);
        System.out.println(Arrays.toString(array));
    }
    //----------------------------------------------------------------------//
    public static void printf(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }
    public static void main6(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6};
        printf(array);
    }
    public static void main5(String[] args) {
        int[] arr = null;
        System.out.println(arr.length);
    }
    public static void main4(String[] args) {
        //借助Java的操作数组的工具类Arrays.toString将参数的数组以字符串的形式输出
        int[] array = {1,2,3,4,5,6};
        String ret = Arrays.toString(array);
        System.out.println(ret);
    }
    public static void main3(String[] args) {
        int[] array = {1,2,3,4,5,6};
        //1.
        for(int i = 0; i < array.length; i++){
            System.out.print(array[i] + " ");
        }
        System.out.println();
        //2.
        for (int x:array) {
            System.out.print(x + " ");
        }
        System.out.println();
    }
    public static void main2(String[] args) {
        int[] array = {1,2,3,4,5,6};
        System.out.println(array.length);//获取数组长度
        array[2] = 100;// 修改数组中的元素
        System.out.println(array[2]);//访问数组中的元素
        System.out.println(array[6]);
    }
    public static void main1(String[] args) {
        int[] array1 = {1,2,3,4,5,6};
        int[] array2 = new int[]{1,2,3,4,5,6};
        int[] array3 = new int[3];
    }
}
